package com.ivt.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.core.util.PwdUtils;
import com.ivt.dao.SpecificDao;
import com.ivt.dao.StudentDao;
import com.ivt.model.Student;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.StudentService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class StudentServiceImpl implements StudentService {

	@Resource
	private StudentDao studentDao;

	@Resource
	private SpecificDao specificDao;

	@Override
	public Student updateStudent(Student student) throws IvtException {
		if (null == student || null == student.getId() || 0 >= student.getId()) {
			throw new IvtException("学生信息不能为空");
		}

		Student original = getStudent(student.getId());

		if (StringUtils.isNotBlank(student.getAccount())
				&& 0 < studentDao.existsByAccountAndId(student.getAccount(),
						student.getId())) {
			throw new IvtException("公告【" + student.getName() + "】已存在");
		}
		if (StringUtils.isNotBlank(student.getPassword())) {
			student.setPassword(PwdUtils.MD256Pws(student.getPassword()));
		} else {
			student.setPassword(original.getPassword());
		}

		student.setStatus(AuditStatus.PASS);
		return studentDao.saveAndFlush(student);
	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryStudentCount(queryCriteria);
	}

	@Override
	public List<Student> queryClassesByCriteria(QueryCriteria queryCriteria)
			throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryStudent(queryCriteria);
	}

	@Override
	public Student delete(Long studentId) throws IvtException {
		if (null == studentId || 0 >= studentId) {
			throw new IvtException("参数错误");
		}

		Student student = new Student();
		student.setId(studentId);
		student.setStatus(AuditStatus.NOT);
		return studentDao.saveAndFlush(student);
	}

	@Override
	public int deletes(Long[] studentIds) throws IvtException {
		if (ArrayUtils.isEmpty(studentIds)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(studentIds);
		Iterable<Student> student = null;
		student = studentDao.findAll(cleanIds);
		int i = 0;
		if (null != student) {
			for (Student cz : student) {
				cz.setStatus(AuditStatus.NOT);
				studentDao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

	@Override
	public Student getStudent(Long studentId) throws IvtException {
		if (null == studentId || 0 >= studentId) {
			throw new IvtException("参数错误");
		}
		return studentDao.findOne(studentId);
	}

	@Override
	public List<Student> getStudent(Set<Long> studentId) throws IvtException {
		if (CollectionUtils.isEmpty(studentId)) {
			throw new IvtException("参数错误");
		}

		Iterable<Student> coll = studentDao.findAll(studentId);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	@Override
	public Student create(Student student) throws IvtException {
		if (null == student) {
			throw new IvtException("学生信息不能为空");
		}
		if (StringUtils.isBlank(student.getName())) {
			throw new IvtException("学生名称不能为空");
		}
		if (StringUtils.isBlank(student.getAccount())) {
			throw new IvtException("学生描述不能为空");
		}

		if (StringUtils.isBlank(student.getPassword())) {
			throw new IvtException("学生密码不能为空");
		}

		long c = studentDao.existsByAccount(student.getAccount());
		if (0 < c) {
			throw new IvtException("学生【" + student.getAccount() + "】已存在");
		}
		student.setPassword(PwdUtils.MD256Pws(student.getPassword()));
		student.setId(null);
		student.setStatus(AuditStatus.PASS);
		return studentDao.saveAndFlush(student);
	}

	@Override
	public Student login(String username, String password) throws IvtException {

		if (StringUtils.isBlank(username) || StringUtils.isBlank(username)) {
			throw new IvtException("登陆失败,账号或密码错误");
		}

		List<Student> students = studentDao.findByAccount(username);

		if (CollectionUtils.isEmpty(students)) {
			throw new IvtException("登陆失败,账号错误");
		}

		Student student = students.get(0);

		if (!PwdUtils.MD256Pws(password).equals(student.getPassword())) {
			throw new IvtException("登陆失败,密码错误");
		}

		return student;
	}

	protected QueryCriteria cleanQueryCriteria(QueryCriteria queryCriteria) {

		if (null == queryCriteria) {
			return null;
		}
		Map<String, Object> criteria = queryCriteria.splitCriteria();

		if (MapUtils.isNotEmpty(criteria)) {

			if (criteria.containsKey("status")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("status")))) {
				AuditStatus status = AuditStatus.findByValue(Integer
						.valueOf(String.valueOf(criteria.get("status"))));
				criteria.put("status", status);
			}
		}

		return queryCriteria;
	}
}
